package com.lst.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.dao.ClientAccomplishMapper;
import com.lst.dao.LoginHistroyMapper;
import com.lst.entity.request.ClientGrowthReqPara;
import com.lst.entity.response.ClientAccomplishResPara;
import com.lst.model.ClientAccomplish;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;

public class ClientAccomplishServlet extends BaseServlet {

    /**
     * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
     */
    private static final long serialVersionUID = 1L;

    private ClientAccomplishMapper clientAccomplishMapper = ctx
	    .getBean(ClientAccomplishMapper.class);

    private LoginHistroyMapper loginHistroyMapper = ctx
	    .getBean(LoginHistroyMapper.class);

    public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
	Date startDate = new Date();
	logger.info("doPost begin: " + startDate);

	ClientAccomplishResPara resPara = new ClientAccomplishResPara();

	try {
	    ClientGrowthReqPara reqPara = getReqPara(request);

	    if (isCheckField(reqPara, resPara)) {
		Integer clientid = Integer.valueOf(reqPara.getUserid());

		Map<String, Object> cgMap = new HashMap<String, Object>();

		cgMap.put("enabled", true);
		cgMap.put("isDeleted", false);
		cgMap.put("clientid", clientid);
		cgMap.put("orderByClause", "cg.createdate desc");

		List<ClientAccomplish> calist = clientAccomplishMapper
			.queryByUser(clientid);
		int daycount = loginHistroyMapper.selectByClient(clientid);

		if (null == calist || calist.size() == 0) {
		    resPara.setCode(CommCode.M_Y000000);
		    resPara.setMessage(CommCode.M_P000011);
		} else {
		    ClientAccomplish ca = calist.get(0);
		    ca.setDayCount(daycount);
		    if (ca.getViewtime().equals("0")) {
			ca.setViewtime("00:00:00");
		    }
		    resPara.setCa(ca);
		    resPara.setCode(CommCode.M_Y000000);
		    resPara.setMessage(CommCode.M_Y000001);
		}
	    }

	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0001);

	    logger.error("doPost error: ", e);
	}

	try {
	    response.setContentType("application/json;charset=utf-8");
	    response.setHeader("pragma", "no-cache");
	    response.setHeader("cache-control", "no-cache");

	    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
		    .excludeFieldsWithoutExposeAnnotation().serializeNulls()
		    .create();
	    String jsonResult = gson.toJson(resPara);
	    logger.info(jsonResult);

	    logger.info("ClientGrowthServlet jsonResult: " + jsonResult);

	    PrintWriter out = response.getWriter();
	    out.print(jsonResult);
	    out.flush();
	    out.close();

	    logger.info("doPost end run(s): "
		    + DateUtil.calLastedTime(startDate));
	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0003);

	    logger.error("doPost error: ", e);
	}
    }

    @Override
    public ClientGrowthReqPara getReqPara(HttpServletRequest request) {

	ClientGrowthReqPara reqPara = null;

	try {
	    BaseRequest baseRequest = RequestUtils.getRequestPara(request,
		    new ClientGrowthReqPara());

	    reqPara = (ClientGrowthReqPara) baseRequest.clone();

	    String userid = request.getParameter("userid");

	    logger.info("reqPara userid: " + userid);

	    reqPara.setUserid(userid);

	} catch (Exception e) {
	    logger.error("getReqPara error: ", e);
	}
	return reqPara;
    }

    @Override
    public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
	boolean flag = false;

	ClientGrowthReqPara cgReqPara = (ClientGrowthReqPara) reqPara;
	ClientAccomplishResPara cgResPara = (ClientAccomplishResPara) resPara;

	if (StringUtils.isEmpty(cgReqPara.getUserid())) {
	    cgResPara.setCode(CommCode.M_A000000);
	    cgResPara.setMessage(CommCode.M_P000002);
	} else {
	    flag = true;
	}
	return flag;
    }
}
